package com.wxy.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.common.core.domain.AjaxResult;
import com.wxy.common.utils.ServletUtils;
import com.wxy.common.utils.StringUtils;
import com.wxy.web.model.Module;
import com.wxy.web.service.IModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2021-06-20
 */
@RestController
@RequestMapping("/admin/Module")
public class ModuleController {
    @Autowired
    private IModuleService moduleService;
    @GetMapping("list")
    public AjaxResult list(Module module)
    {
        Integer pageNum = ServletUtils.getParameterToInt("page",1);
        Integer pageSize = ServletUtils.getParameterToInt("limit",10);

        LambdaQueryWrapper<Module> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.isNotEmpty(module.getName()))
        {
            lambdaQueryWrapper.like(Module::getName , module.getName());
        }
        if(StringUtils.isNotEmpty(module.getUrl()))
        {
            lambdaQueryWrapper.like(Module::getUrl , module.getUrl());
        }
        if(StringUtils.isNotEmpty(module.getPermissionCode()))
        {
            lambdaQueryWrapper.like(Module::getPermissionCode , module.getPermissionCode());
        }

        Page<Module> page = new Page<>(pageNum , pageSize);
        page.addOrder(OrderItem.desc("id"));
        IPage<Module> iPage = this.moduleService.page(page,lambdaQueryWrapper);

        HashMap<String,Object> map = new HashMap<>();
        map.put("list",iPage.getRecords());
        map.put("count",iPage.getTotal());

        AjaxResult result = AjaxResult.success();
        result.put("data",map);
        return result;
    }


    @GetMapping("info")
    public AjaxResult info(Integer id)
    {
        Module module = moduleService.getById(id);
        AjaxResult result = AjaxResult.success();
        result.put("data",module);
        return result;
    }

    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody Module module)
    {
        Boolean success = moduleService.save(module);
        AjaxResult ajaxResult = null;
        if(success)
            ajaxResult = AjaxResult.success();
        else
            ajaxResult = AjaxResult.error();

        return ajaxResult;
    }

    @PostMapping("edit")
    public AjaxResult edit(@Validated @RequestBody Module module)
    {
        Boolean success = moduleService.updateById(module);
        AjaxResult ajaxResult = null;
        if(success)
            ajaxResult = AjaxResult.success();
        else
            ajaxResult = AjaxResult.error();

        return ajaxResult;
    }

    @PostMapping("del/{id}")
    public AjaxResult del(@PathVariable Integer id)
    {
        Boolean success = moduleService.removeById(id);
        AjaxResult ajaxResult = null;
        if(success)
            ajaxResult = AjaxResult.success();
        else
            ajaxResult = AjaxResult.error();

        return ajaxResult;
    }
}
